package com.linjiahao.userservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.linjiahao.commonutils.JwtUtils;
import com.linjiahao.commonutils.ResultMap;
import com.linjiahao.userservice.entity.OnlineLive;
import com.linjiahao.userservice.entity.vo.LiveVo;
import com.linjiahao.userservice.entity.vo.onlineVo;
import com.linjiahao.userservice.service.OnlineLiveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-09-19
 */
@RestController
@RequestMapping("/userservice/online-live")
public class OnlineLiveController {

    @Value("${shipin.key}")
    String key;

    @Autowired
    private OnlineLiveService onlineLiveService;

    @PostMapping("getAddress")
    public ResultMap getAddress(@RequestBody LiveVo liveVo, HttpServletRequest request){
        String memberId= JwtUtils.getMemberIdByJwtToken(request);
        OnlineLive name = onlineLiveService.getOne(new QueryWrapper<OnlineLive>().eq("name", liveVo.getName()));
        if(name==null){
            String flv=new StringBuilder().append("http://linjiahao.xyz/live/").append(liveVo.getName()+".flv").toString();
            String mu38=new StringBuilder().append("http://linjiahao.xyz/live/").append(liveVo.getName()+".m3u8").toString();
            onlineLiveService.save(new OnlineLive().setLiveTitle(liveVo.getTitle()).setPushIp(flv)
                    .setMemberId(memberId).setIntroduce(liveVo.getIntroduce())
                    .setName(liveVo.getName()).setWatchIp(mu38).setStatus(0));
            return ResultMap.ok();
        }
        return ResultMap.error().message("当前直播号已存在");
    }

    @GetMapping("getonline")
    public ResultMap getOnline(){
        List<onlineVo> all = onlineLiveService.getAll();
        return ResultMap.ok().data("online_all",all);
    }

    @GetMapping("getById/{id}")
    public ResultMap getById(@PathVariable String id){
        OnlineLive byId = onlineLiveService.getById(id);
        return ResultMap.ok().data("flv",byId.getPushIp()).data("mu38",byId.getWatchIp()).data("status",byId.getStatus());
    }

    @DeleteMapping("deleteById/{id}")
    public ResultMap deleteById(@PathVariable String id){
        System.out.println(id);
        boolean b = onlineLiveService.removeById(id);
        if(b){
            return ResultMap.ok();
        }
        return ResultMap.error();
    }

    @GetMapping("changeOnpen/{id}")
    public ResultMap changeOpne(@PathVariable String id){
        OnlineLive onlineLive = new OnlineLive();
        onlineLive.setId(id);
        onlineLive.setStatus(1);
        onlineLiveService.updateById(onlineLive);
        return ResultMap.ok();
    }
}

